//
//  GS_SportsViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//
#define URL @"http://api.iclient.ifeng.com/ClientNews?id=DZPD,FOCUSDZPD&page=1&gv=4.5.1&av=4.5.1&uid=352324075088508&deviceid=352324075088508&proid=ifengnews&os=android_22&df=androidphone&vt=5&screen=1080x1920&publishid=2012"

#import "GS_SportsViewController.h"
#import "Masonry.h"
#import "GS_SportsTableViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "GS_SportsModel.h"
#import "GS_SportsItemModel.h"
#import "GS_SportsSlideTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface GS_SportsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIView *headerV;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *itemArr;

@end

@implementation GS_SportsViewController

- (void)dealloc
{
    [_tableV release];
    [_imageV release];
    [_arr release];
    [_itemArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    [self createTableViews];
    [self getData];
    
}

- (void)createTableViews
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    [self.view addSubview:self.tableV];
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(20);
//        make.bottom.equalTo(self.view);
        make.width.equalTo(self.view);
        make.bottom.mas_equalTo(self.view).with.offset(-49);
        
        
    }];
    
    
    
    
    
    
    
//    [_imageV release];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.imageV = [UIImageView new];
    self.imageV.backgroundColor = [UIColor greenColor];
    self.tableV.tableHeaderView = self.imageV;
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.tableV);
        make.width.mas_equalTo(self.tableV);
        make.height.mas_equalTo(@200);
        
    }];
    return self.imageV;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return self.arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    GS_SportsItemModel *itemModel = self.arr[indexPath.row];
    if ([itemModel.type isEqualToString:@"doc"]) {
        static NSString *cellStr = @"cell";
        GS_SportsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (nil == cell) {
            cell = [[GS_SportsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        cell.titleL.text = itemModel.title;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:itemModel.thumbnail] placeholderImage:nil];
        cell.backgroundColor = [UIColor purpleColor];
        return cell;
    } else {
        static NSString *slideCellStr = @"slideCell";
        GS_SportsSlideTableViewCell *slideCell = [tableView dequeueReusableCellWithIdentifier:slideCellStr];
        if (nil == slideCell) {
            slideCell = [[GS_SportsSlideTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:slideCellStr];
        }
        slideCell.backgroundColor = [UIColor redColor];
        return slideCell;
    }
    
}

- (void)getData
{
    self.arr = [NSMutableArray array];
    self.itemArr = [NSMutableArray array];
    
    [GoodManNews_AFNetTool getNetWithURL:URL body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {

        for (NSMutableDictionary *dic in responseObject) {
     
            GS_SportsModel *model = [[GS_SportsModel alloc] initWithDic:dic];
            
//            NSLog(@"%@", model.type);
    
            if ([model.type isEqualToString:@"list"]) {
//                NSLog(@"%@", model.item);
                NSMutableArray *listArr = [NSMutableArray array];
                listArr = [dic objectForKey:@"item"];
                
                for (NSMutableDictionary *itemDic in listArr) {
                    GS_SportsItemModel *itemModel = [[GS_SportsItemModel alloc] initWithDic:itemDic];
                    NSLog(@"+++++%@", itemDic);
//                    if ([itemModel.type isEqualToString:@"doc"]) {
                    [self.arr addObject:itemModel];
//                    } else {
                    
//                    }
                }
            }
        }
//        NSLog(@"%ld", self.arr.count);
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
